package com.fit.piris.pages.fanpage;

import java.util.Date;
import java.util.List;

import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.piris.dao.fanpage.FanPageDAO;
import com.fit.piris.dao.user.fanpage.UserFanPageDAO;
import com.fit.piris.entities.fanpage.FanPage;
import com.fit.piris.entities.user.User;
import com.fit.piris.entities.user.fanpage.UserFanPage;
import com.fit.piris.services.Authenticator;

public class Browse {

	@Inject
	private FanPageDAO fanPageDAO;
	
	@Property
	private List<FanPage> fanPages;
	@Property
	private FanPage fanPage;
	@Inject
	private UserFanPageDAO userFanPageDAO;
	@Inject
	private Authenticator authenticator;
	
	Object onActivate(){
		fanPages = fanPageDAO.findAll();
		return null;
	}
	
	public boolean isLike(Long idFanPage){
		UserFanPage uu = userFanPageDAO.findByID(authenticator.getLoggedUser().getId(), idFanPage);
		System.out.println("FAN JE STRANE " + uu.getFanPage().getName());
		if (uu !=null ) {
			System.out.println("IMA STRANICA LIKE!");
			return true;
			
		}
		System.out.println("NEMA STRANICA LIKE");
		return false;
	}
	
	@OnEvent(component="like")
	Object onLike(Long idFanPage){
		UserFanPage u = new UserFanPage();
		u.setDateUnrol(new Date());
		u.setFanPage(fanPageDAO.findByID(idFanPage));
		u.setUser(authenticator.getLoggedUser());
		userFanPageDAO.save(u);
		return null;
	}
	@OnEvent(component="unlike")
	Object onUnLike(Long idFanPage){
		User uu = authenticator.getLoggedUser();
		System.out.println(uu.getFullname());
		UserFanPage u = userFanPageDAO.findByID(uu.getId(), idFanPage);
		userFanPageDAO.delete(u);
		return null;
	}
	
}
